package com.devframe.sysconfig.storage.operate.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.devframe.sys.util.assertion.Assert;
import com.devframe.sysconfig.service.CodeService;
import com.devframe.sysconfig.service.CtrlTableService;
import com.devframe.sysconfig.service.SysParamService;
import com.devframe.sysconfig.storage.operate.WriterCacheData;
import com.devframe.sysconfig.storage.operate.SysConfigDataOperate;

@Component(value="writerCacheData")
public class WriterCacheDataImpl extends SysConfigDataOperate  implements WriterCacheData{

	@Autowired
	private CodeService codeService;
	@Autowired
	private SysParamService sysParamService;
	@Autowired
	private CtrlTableService ctrlTableService;
	
	public void clearCodeTableData(){
		super.getSysConfigDataCache().clearCodeTableData();
	}

	public void addCodeTableSet(String key, Map val) {
		//加入同步，并判断是否存在
		super.getSysConfigDataCache().addCodeTableSet(key, val);
	}

	public synchronized Map addCodeTable(String codeTableName) {
		//加入同步，并判断是否存在
		Map map=null;
		 if (!super.getSysConfigDataCache().containsKeyInCodeTable(codeTableName)) {
			Map codeTable=super.getSysConfigDataCache().getCodeTableSet(codeTableName);
			map=this.codeService.loadCodeTableData(codeTable);
			super.getSysConfigDataCache().addCodeTable(codeTableName, map);
		 }
		return map;
	}

	public void bindCodeServiceHandle(CodeService codeService) {
		this.codeService=codeService;
		
	}
	
	public void cleanSysParamData(){
		super.getSysConfigDataCache().clearSysParamData();
	}
	public void addSysParam(String key, String value) {
		//加入同步，并判断是否存在
		super.getSysConfigDataCache().addSysParam(key, value);
	}
	
	public  void updateSysParam(String key, String value){
		//加入同步
		
		//更新到库
		this.sysParamService.updateSysParam(key, value);
		
		//更新到缓存中
		super.getSysConfigDataCache().updateSysParam(key, value);
	}
	
	public void setSysParam(String key, String value){
		//加入同步
		this.getSysConfigDataCache().setSysParam(key, value);
	}
	
	public void bindSysParamService(SysParamService sysParamService) {
		this.sysParamService=sysParamService;
	}
	
	/**
	 * 查询缓存控制表记录
     * @param tableAlias tableAlias
     * @return List
     * 
     */
	public synchronized List addCtrlTable(String tableAlias) {
		//加入同步判断
		Assert.notNull(tableAlias, "表码表别名不能为null");
		Assert.state(this.getSysConfigDataCache().containsKeyInCtrlTableSet(tableAlias), "没有配置的控制表:"+tableAlias);
		if(!this.getSysConfigDataCache().containsKeyInCtrlTable(tableAlias)){
			List list=this.ctrlTableService.loadCtrlTable(tableAlias);
		    super.getSysConfigDataCache().addCtrlTable(tableAlias, list);
		}
		return (List)super.getSysConfigDataCache().getCtrlTable(tableAlias);
	}
	

	public void bindCtrlTableService(CtrlTableService ctrlTableService) {
		this.ctrlTableService=ctrlTableService;
		
	}


	public void cleanCtrlTableData() {
		super.getSysConfigDataCache().clearCtrlTableData();
	}
	
	public void addCtrlTableSet(String tableAlias,String tableName){
		super.getSysConfigDataCache().addCtrlTableSet(tableAlias, tableName);
	}



	public void removeCtrlTable(String tableAlias) {
		if(this.getSysConfigDataCache().containsKeyInCtrlTable(tableAlias)){
			this.getSysConfigDataCache().removeCtrlTable(tableAlias);
		}
	}
    
    
	
}
